//
//  SkillPicker.m
//  APJ Resource
//
//  Created by Richard Zhao on 2/12/14.
//  Copyright (c) 2014 APJ. All rights reserved.
//

#import "SkillPicker.h"
#import "HRStore.h"
#import "Skill.h"

@interface SkillPicker ()
{
    NSArray *dataSource;
}
@end

@implementation SkillPicker
@synthesize skillCategory, selectedSkills;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    dataSource = [[HRStore sharedStore] skillUnderCategory:skillCategory];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SkillCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifier];
    }
    
    Skill *skill = (Skill *)[dataSource objectAtIndex:[indexPath row]];
    [[cell textLabel] setText:[skill skillCode]];
//    [cell setIndentationLevel:[d deptId]];
    
    if ([selectedSkills containsObject:[skill skillCode]]) {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }
    
    
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if ([cell accessoryType] == UITableViewCellAccessoryCheckmark) {
        [cell setAccessoryType:UITableViewCellAccessoryNone];
        [selectedSkills removeObject:cell.textLabel.text];
    } else {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
        [selectedSkills addObject:cell.textLabel.text];
    }
}

@end
